//
//  HBPayController.m
//  HeartBeat
//
//  Created by karong on 2023/9/14.
//

#import "HBPayController.h"
#import "UIButton+ImageAlign.h"
#import "HBPayCoinCell.h"
#import "HBPayCoinModel.h"

@interface HBPayController () 
@property (weak, nonatomic) IBOutlet UICollectionView *listView;
@property (nonatomic, strong) NSMutableArray * dataArray;
@property (weak, nonatomic) IBOutlet UIView *mainView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *mainViewBottom;
@property (weak, nonatomic) IBOutlet UILabel *coinsLabel;
@property (weak, nonatomic) IBOutlet UILabel *totalCoinLabel;
@property (weak, nonatomic) IBOutlet UIButton *autoPaybtn;
@property (weak, nonatomic) IBOutlet UIButton *payBtn;
@property (nonatomic, assign) NSInteger selectIndex;
@property (nonatomic, strong) HBPayModel * payModel;
@end

@implementation HBPayController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.dataArray = [NSMutableArray new];
    HBPayCoinModel * payCoinModel = [HBPayCoinModel new];
    payCoinModel.chapter_num = 1;
    payCoinModel.discountedPrice = self.chapter.coins;
    [self.dataArray addObject:payCoinModel];
    [self loadPayListRequest];
    self.selectIndex = 0;
    [self layoutMainView];
    [self setMainViewData];
    [self addObserver];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.mainViewBottom.constant = 0;
        [self.view uploadLayout];
    });
    [HBEvenManager event:@"chapter_gold" evenLogType:(HBEvenLogType_Show)];
}

- (void)styleDidChange {
    self.view.backgroundColor = UIColor.clearColor;
}

- (void)addObserver {
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(layoutMainView) name:HBRefreshUserInfoNotification object:nil];
}

- (void)layoutMainView {
    self.coinsLabel.text = SF(WSLocalized(@"書幣餘額:%ld書幣"), (long)appUserCenter.coins);
    self.autoPaybtn.selected = appConfig.isAutoPay;
    [self.autoPaybtn setButtonImageTitleStyle:(WSButtonImageTitleStyleLeft) padding:6];
}

- (void)setMainViewData {
    HBPayCoinModel * payCoinModel = self.dataArray[self.selectIndex];
    self.totalCoinLabel.text = SF(WSLocalized(@"%ld書幣"), payCoinModel.discountedPrice);
}

- (void)loadPayListRequest {
    [HBToast loading];
    [HBRequest loadPayListWithChapter_id:self.chapter.id success:^(id obj) {
        [HBToast dismiss];
        self.payModel = obj;
        [self.payModel calculationPrice];
        [self.dataArray addObjectsFromArray:self.payModel.buy_percent];
        [self.listView reloadData];
    } failed:^(NSURLResponse *response, NSError *error, id responseObject) {
        
    }];
}
- (IBAction)payAction:(id)sender {
    if ([self.delegate respondsToSelector:@selector(payControllerPayActionWithChapter:num:)]) {
        HBPayCoinModel * payCoinModel = self.dataArray[self.selectIndex];
        [self.delegate payControllerPayActionWithChapter:self.chapter num:payCoinModel.chapter_num];
    }
}

- (IBAction)rechargeAction:(id)sender {
    if ([self.delegate respondsToSelector:@selector(payControllerRechargeActionWithChapter:)]) {
        [self.delegate payControllerRechargeActionWithChapter:self.chapter];
    }
}

- (IBAction)autoAction:(UIButton *)sender {
    sender.selected = !sender.selected;
    appConfig.isAutoPay = sender.selected;
}
#pragma mark UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HBPayCoinCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HBPayCoinCell" forIndexPath:indexPath];
    if (indexPath.row == 0) {
        [cell setDataWithCoin:self.chapter.coins select:self.selectIndex == indexPath.row];
    }
    else {
        [cell setDataWithObject:self.dataArray[indexPath.row] select:self.selectIndex == indexPath.row];
    }
    
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGSize size = CGSizeZero;
    size.width = 75;
    size.height = 75;
    return size;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    self.selectIndex = indexPath.row;
    [self setMainViewData];
    [self.listView reloadData];
}


@end
